# -*- coding: utf-8 -*-
import sys
sys.path.append("../..")
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode

class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "太平乐悠悠境内旅行险"

    # 生成产品数据
    def genProductInfo(self):
        plan = {
            "planA":{
                "days":["1-3d","4-5d","6-7d","8-10d","11-15d","16-30d"],
                "people_count":[str(i) for i in range(1,8)],
                "accident_death":["200000"],
                "vacation_death":["200000"],
                "accident_medical":["20000"],
                "sudden_death":["50000"],
                "sudden_medical":["10000"],
                "airplane_death":["300000"],
                "ship_death":["100000"],
                "train_death":["100000"],
                "passenger_death":["100000"]
            },
            "planB": {
                "days": ["1-3d", "4-5d", "6-7d", "8-10d", "11-15d", "16-30d"],
                "people_count": [str(i) for i in range(1, 8)],
                "accident_death": ["300000"],
                "vacation_death": ["300000"],
                "accident_medical": ["50000"],
                "sudden_death": ["70000"],
                "sudden_medical": ["20000"],
                "airplane_death": ["600000"],
                "ship_death": ["200000"],
                "train_death": ["200000"],
                "passenger_death": ["200000"]
            },
            "planC": {
                "days": ["1-3d", "4-5d", "6-7d", "8-10d", "11-15d", "16-30d"],
                "people_count": [str(i) for i in range(1, 8)],
                "accident_death": ["500000"],
                "vacation_death": ["500000"],
                "accident_medical": ["100000"],
                "sudden_death": ["100000"],
                "sudden_medical": ["30000"],
                "airplane_death": ["1000000"],
                "ship_death": ["300000"],
                "train_death": ["300000"],
                "passenger_death": ["300000"]
            },
        }
        for k in plan:
            for v in product( plan[k]["days"],plan[k]["people_count"],plan[k]["accident_death"],plan[k]["vacation_death"],plan[k]["accident_medical"],plan[k]["sudden_death"],plan[k]["sudden_medical"],plan[k]["airplane_death"],plan[k]["ship_death"],plan[k]["train_death"],plan[k]["passenger_death"]):
                data = Helper.formatProductInfo( plan[k],v )
                yield  data
    # 执行爬虫
    def requestJson(self):
        for v in self.genProductInfo():
            params = '''
            pageName: productAdd
            goods_id: 62553
            merchant_id: B2C
            toubaoli_money: 
            productCode: 201906111000001
            productId: 200b8ada-a4bd-44ba-a721-da2d8bc5b89a
            riskCode: 201906111000000
            productName: 太平乐悠悠境内旅行险
            insuredPrice: 128
            insured.birthday: 
            templateType: 1
            employee_id: 
            channel: GWTPGW-CPTJ-A12
            empNo: 
            newsCode: 
            twoMonthInsuredPrice: 256.00
            loginUserId: 
            policy.startDate: 
            policy.endDate: 
            trialBySexAndAge: false
            policy.dateRemark: 30
            policy.planType: 02
            extend2: 02
            policy.insuredNumber: 05
            policy.showMessage: false
            person.mobilenum: 承保短信将发送至您的手机
            '''
            data = Helper.perfectJson( params )
            if v["accident_death"] == "200000":
                planType = "01"
            elif v["accident_death"] == "300000":
                planType = "02"
            else:
                planType = "03"
            data["policy.dateRemark"] = v["days"].split("-")[1].replace("d","")
            data["policy.planType"] = planType
            data["extend2"] = planType
            data["policy.insuredNumber"] = "0" + v["people_count"]
            payload = urlencode( data ).replace("+","")
            r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
            if r.status_code == 200 and "totalPremium" in r.text:
                price = Helper.decimalParse( r.json()["totalPremium"])
                data = {
                    "name":self.name,
                    "company":"中国太平",
                    "price":price
                }
                data.update( v )
                Helper.mergeTable(self.name,self.mysql,data )
